using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using NetProtocol.Client;
using NetProtocol.POD;
using static NetProtocol.Client.CNetFormation;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 类名 : 模块 - 编队
    /// 作者 : Canyon / 龚阳辉
    /// 日期 : 2024-06-04 15:56
    /// 功能 : 继承网络请求协议 等价于 Manager
    /// </summary>
    public class FormationModule : Singleton<FormationModule>, CNetFormation
    {
        private FormationModule() { }

        private FormationModuleData moduleData => GameDataCenter.PlayerData.formation;
        private int defFormId => moduleData.defFormId;

        public void InitModule(PlayerPOD playerPOD)
        {
            FormationModuleData modueData = new FormationModuleData(playerPOD.defFormId);
            GameDataCenter.PlayerData.formation = modueData;
            var list = playerPOD.forms;
            foreach (var item in list)
            {
                this.ChangeData(item);
            }
        }

        public void ClearModule()
        {
            var data = GameDataCenter.PlayerData.formation;
            GameDataCenter.PlayerData.formation = null;
            data?.Clear();
        }

        #region Net层
        static public CNetFormation NetCore => Instance;
        void CNetFormation.SC_notifyFormation(ISession session, FormationPOD formationPOD)
        {
            this.ChangeData(formationPOD);
            MsgDispatcher.Broadcast(MsgEventType.OneFormationChanged, formationPOD);
        }

        public void SC_setDefaultResult(ISession session, ref SetDefaultResultData data)
        {
            if (data.IsSuccess)
            {
                moduleData.UpdateDefaultFomrId(data.formId);
            }
            NetCore._SC_setDefaultResult(session, ref data);
        }

        ISession CNetFormation.GetSession()
        {
            return GameServerModule.Instance.CurrSession;
        }
        #endregion

        protected void ChangeData(FormationPOD pod)
        {
            FormationData dCahce = this.moduleData.GetFormation(pod.formId);
            if (dCahce == null)
            {
                LogGame.LogError($"同步了不存在的队伍，FormId = {pod.formId}");
                return;
            }
            dCahce.Sync(pod);
        }

        public FormationData GetFormationByIndex(int index)
        {
            return moduleData.GetFormationByIndex(index);
        }

        public FormationData GetFormation(int formId)
        {
            return this.GetFormationByIndex(formId - 1);
        }

        public bool IsDefaultFormation(int formId)
        {
            return this.defFormId == formId;
        }

        public int GetCountRegularHero(int formId)
        {
            var formData = this.GetFormation(formId);
            if (formData == null)
            {
                return 0;
            }

            int count = 0;
            foreach (var item in formData.heros)
            {
                if (item.Value.isHasHero && item.Key <= APIUIFormation.FormRegularHeroCount)
                {
                    count++;
                }
            }
            return count;
        }

        public bool IsHasRegularHero(int formId)
        {
            int cnt = this.GetCountRegularHero(formId);
            return cnt > 0;
        }

        public string GetFormationName(FormationData formData)
        {
            if (string.IsNullOrEmpty(formData.formName))
            {
                return APIUIFormation.GetFormTabName(formData.formId);
            }
            return formData.formName;
        }
    }
}
